#!/bin/bash
# The following are default values.
# They can be overridden by supplying the -e VAR=value option to the containerizer.

# The timezone this container is running in
TZ=${TZ:-Etc/UTC}

# User and group id for the user running valheim-server
PUID=${PUID:-0}
PGID=${PGID:-0}

# Valheim dedicated server related values
WORLD_NAME=${WORLD_NAME:-Dedicated}
SERVER_NAME=${SERVER_NAME:-My Server}
SERVER_PORT=${SERVER_PORT:-2456}
SERVER_QUERY_PORT=$((SERVER_PORT + 1))
SERVER_PASS=${SERVER_PASS-secret}
if [ "${VPCFG_Server_disableServerPassword:-}" = true ]; then
	SERVER_PASS=""
fi

SERVER_ARGS=${SERVER_ARGS:-}
SERVER_PUBLIC=${SERVER_PUBLIC:-1}
[ "$SERVER_PUBLIC" = true  ] && SERVER_PUBLIC=1
[ "$SERVER_PUBLIC" = false ] && SERVER_PUBLIC=0

# steamcmd.sh arguments
STEAMCMD_ARGS=${STEAMCMD_ARGS-validate}

# Public Test
PUBLIC_TEST=${PUBLIC_TEST:-false}
if [ "$PUBLIC_TEST" = true ]; then
    STEAMCMD_ARGS="$STEAMCMD_ARGS -beta public-test -betapassword yesimadebackups"
fi

# Crossplay
CROSSPLAY=${CROSSPLAY:-false}

# Debug Flags
# Flag to wipe all downloaded server data on startup (config is untouched)
DEBUG_START_FRESH=${DEBUG_START_FRESH:-false}
# Flat to make valheim-plus-updater reinstall the V+ ZIP over vanilla server
DEBUG_REINSTALL_VALHEIM_PLUS=${DEBUG_REINSTALL_VALHEIM_PLUS:-false}
# Flat to make bepinex-updater reinstall the BepInEx ZIP over vanilla server
DEBUG_REINSTALL_BEPINEX=${DEBUG_REINSTALL_BEPINEX:-false}

# How we behave when checking for whether the server is idle, if it is not
# public (which would mean that we could just query for user activity).
# How long to wait for incoming UDP datagrams, in seconds
IDLE_DATAGRAM_WINDOW=3
# How many incoming UDP datagrams we can tolerate before declaring the server
# as un-idle (allows some buffer for UDP querying or random UDP pings)
IDLE_DATAGRAM_MAX_COUNT=30

# How often we check for Valheim server updates
# This used to be 900 (15 min) and is here for backwards
# compatibility reasons. We now set it to 10 years by default
# and use a cron that sends SIGHUP instead
UPDATE_INTERVAL=${UPDATE_INTERVAL:-315360000}
# When we check for updates
UPDATE_CRON=${UPDATE_CRON-*/15 * * * *}
UPDATE_IF_IDLE=${UPDATE_IF_IDLE:-true}

# What time we restart the valheim-server
# This is usful to mitigate the effects of memory/resource leaks
RESTART_CRON=${RESTART_CRON-10 5 * * *}
RESTART_IF_IDLE=${RESTART_IF_IDLE:-true}

# World backup related settings
BACKUPS=${BACKUPS:-true}
# Legacy interval variable used to be 3600 (1h)
BACKUPS_INTERVAL=${BACKUPS_INTERVAL:-315360000}
BACKUPS_CRON=${BACKUPS_CRON-5 * * * *}
BACKUPS_DIRECTORY=${BACKUPS_DIRECTORY:-/config/backups}
BACKUPS_MAX_AGE=${BACKUPS_MAX_AGE:-3}
BACKUPS_MAX_COUNT=${BACKUPS_MAX_COUNT:-0}
BACKUPS_IF_IDLE=${BACKUPS_IF_IDLE:-true}
BACKUPS_IDLE_GRACE_PERIOD=${BACKUPS_IDLE_GRACE_PERIOD:-3600}
BACKUPS_ZIP=${BACKUPS_ZIP:-true}

# Supervisor http
SUPERVISOR_HTTP=${SUPERVISOR_HTTP:-false}
SUPERVISOR_HTTP_PORT=${SUPERVISOR_HTTP_PORT:-9001}
SUPERVISOR_HTTP_USER=${SUPERVISOR_HTTP_USER:-admin}
SUPERVISOR_HTTP_PASS=${SUPERVISOR_HTTP_PASS:-}

# Status http
STATUS_HTTP=${STATUS_HTTP:-false}
STATUS_HTTP_PORT=${STATUS_HTTP_PORT:-80}
STATUS_HTTP_CONF=${STATUS_HTTP_CONF:-/config/httpd.conf}
STATUS_HTTP_HTDOCS=${STATUS_HTTP_HTDOCS:-/opt/valheim/htdocs}
STATUS_HTTP_HTDOCS=${STATUS_HTTP_HTDOCS%/}

# Mod support
VALHEIM_PLUS=${VALHEIM_PLUS:-false}
VALHEIM_PLUS_REPO=${VALHEIM_PLUS_REPO:-Grantapher/ValheimPlus}
VALHEIM_PLUS_RELEASE=${VALHEIM_PLUS_RELEASE:-latest}
VALHEIM_PLUS_CFG_ENV_PREFIX=${VALHEIM_PLUS_CFG_ENV_PREFIX:-VPCFG_}
BEPINEX=${BEPINEX:-false}
BEPINEX_CFG_ENV_PREFIX=${BEPINEX_CFG_ENV_PREFIX:-BEPINEXCFG_}

# Permissions
PERMISSIONS_UMASK=${PERMISSIONS_UMASK:-022}
DEFAULT_DIRECTORY_PERMISSIONS=${DEFAULT_DIRECTORY_PERMISSIONS:-$(printf "%#o" $(( 8#0777-8#$PERMISSIONS_UMASK )))}
DEFAULT_FILE_PERMISSIONS=${DEFAULT_FILE_PERMISSIONS:-$(printf "%#o" $(( 8#0666-8#$PERMISSIONS_UMASK )))}
CONFIG_DIRECTORY_PERMISSIONS=${CONFIG_DIRECTORY_PERMISSIONS:-$DEFAULT_DIRECTORY_PERMISSIONS}
CONFIG_FILE_PERMISSIONS=${CONFIG_FILE_PERMISSIONS:-$DEFAULT_FILE_PERMISSIONS}
WORLDS_DIRECTORY_PERMISSIONS=${WORLDS_DIRECTORY_PERMISSIONS:-$DEFAULT_DIRECTORY_PERMISSIONS}
WORLDS_FILE_PERMISSIONS=${WORLDS_FILE_PERMISSIONS:-$DEFAULT_FILE_PERMISSIONS}
BACKUPS_DIRECTORY_PERMISSIONS=${BACKUPS_DIRECTORY_PERMISSIONS:-$DEFAULT_DIRECTORY_PERMISSIONS}
BACKUPS_FILE_PERMISSIONS=${BACKUPS_FILE_PERMISSIONS:-$DEFAULT_FILE_PERMISSIONS}
VALHEIM_PLUS_CONFIG_DIRECTORY_PERMISSIONS=${VALHEIM_PLUS_CONFIG_DIRECTORY_PERMISSIONS:-$DEFAULT_DIRECTORY_PERMISSIONS}
VALHEIM_PLUS_CONFIG_FILE_PERMISSIONS=${VALHEIM_PLUS_CONFIG_FILE_PERMISSIONS:-$DEFAULT_FILE_PERMISSIONS}
BEPINEX_CONFIG_DIRECTORY_PERMISSIONS=${BEPINEX_CONFIG_DIRECTORY_PERMISSIONS:-$DEFAULT_DIRECTORY_PERMISSIONS}
BEPINEX_CONFIG_FILE_PERMISSIONS=${BEPINEX_CONFIG_FILE_PERMISSIONS:-$DEFAULT_FILE_PERMISSIONS}

# Server log filter
# Valheim server logs empty lines as well as lines containing a single space.
# It also logs a repeating line "(Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35)".
# We filter all of this by default.
VALHEIM_LOG_FILTER_VERBOSE=${VALHEIM_LOG_FILTER_VERBOSE:-2}
export VALHEIM_LOG_FILTER_EMPTY=${VALHEIM_LOG_FILTER_EMPTY:-true}
export VALHEIM_LOG_FILTER_UTF8=${VALHEIM_LOG_FILTER_UTF8:-true}
export VALHEIM_LOG_FILTER_MATCH=${VALHEIM_LOG_FILTER_MATCH- }
export VALHEIM_LOG_FILTER_STARTSWITH=${VALHEIM_LOG_FILTER_STARTSWITH-(Filename:}
export VALHEIM_LOG_FILTER_STARTSWITH_AssertionFailed=${VALHEIM_LOG_FILTER_STARTSWITH_AssertionFailed-src/steamnetworkingsockets/clientlib/steamnetworkingsockets_lowlevel.cpp}
if [ "$VALHEIM_PLUS" = true ] || [ "$BEPINEX" = true ]; then
    export VALHEIM_LOG_FILTER_STARTSWITH_BepInEx=${VALHEIM_LOG_FILTER_STARTSWITH_BepInEx-Fallback handler could not load library}
fi

# Syslog settings
SYSLOG_REMOTE_HOST=${SYSLOG_REMOTE_HOST:-}
SYSLOG_REMOTE_PORT=${SYSLOG_REMOTE_PORT:-514}
SYSLOG_REMOTE_AND_LOCAL=${SYSLOG_REMOTE_AND_LOCAL:-true}

# Server status
SERVER_STATUS_FILE=${SERVER_STATUS_FILE:-/var/run/valheim/valheim-server.status}

# Hooks
PRE_SUPERVISOR_HOOK=${PRE_SUPERVISOR_HOOK:-}
PRE_BOOTSTRAP_HOOK=${PRE_BOOTSTRAP_HOOK:-}
POST_BOOTSTRAP_HOOK=${POST_BOOTSTRAP_HOOK:-}
PRE_BACKUP_HOOK=${PRE_BACKUP_HOOK:-}
POST_BACKUP_HOOK=${POST_BACKUP_HOOK:-}
PRE_UPDATE_CHECK_HOOK=${PRE_UPDATE_CHECK_HOOK:-}
POST_UPDATE_CHECK_HOOK=${POST_UPDATE_CHECK_HOOK:-}
PRE_START_HOOK=${PRE_START_HOOK:-}
POST_START_HOOK=${POST_START_HOOK:-}
PRE_RESTART_HOOK=${PRE_RESTART_HOOK:-}
POST_RESTART_HOOK=${POST_RESTART_HOOK:-}
PRE_SERVER_LISTENING_HOOK=${PRE_SERVER_LISTENING_HOOK:-}
POST_SERVER_LISTENING_HOOK=${POST_SERVER_LISTENING_HOOK:-}
PRE_SERVER_RUN_HOOK=${PRE_SERVER_RUN_HOOK:-}
POST_SERVER_RUN_HOOK=${POST_SERVER_RUN_HOOK:-}
PRE_SERVER_SHUTDOWN_HOOK=${PRE_SERVER_SHUTDOWN_HOOK:-}
POST_SERVER_SHUTDOWN_HOOK=${POST_SERVER_SHUTDOWN_HOOK:-}
PRE_BEPINEX_CONFIG_HOOK=${PRE_BEPINEX_CONFIG_HOOK:-}
POST_BEPINEX_CONFIG_HOOK=${POST_BEPINEX_CONFIG_HOOK:-}

# Adminlist/Bannedlist/Permittedlist IDs
ADMINLIST_IDS=${ADMINLIST_IDS:-}
BANNEDLIST_IDS=${BANNEDLIST_IDS:-}
PERMITTEDLIST_IDS=${PERMITTEDLIST_IDS:-}
